package xkp.lesson.dao;

import xkp.lesson.entity.BankAccountEntity;

import java.math.BigDecimal;
import java.sql.*;
import java.util.List;

public class BankAccountDao implements BaseDao<BankAccountEntity> {
    @Override
    public List<BankAccountEntity> selectAll() throws SQLException {
        return null;
    }

    @Override
    public BankAccountEntity selectById(Integer id) throws SQLException {
        return null;
    }



    public BankAccountEntity selectByName(Connection connection,String name) throws SQLException {
        BankAccountEntity bankAccountEntity = null;
        String sql = "select * from bank_account where account_name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        ResultSet set = statement.executeQuery();
        while(set.next()){
             Integer accountId = set.getInt("account_id");
             String accountName = set.getString("account_name");
             BigDecimal accountMoney = set.getBigDecimal("account_money");
             Timestamp createTime = set.getTimestamp("create_time");
             Timestamp updateTime = set.getTimestamp("update_time");
            bankAccountEntity = new BankAccountEntity(
                    accountId,accountName,accountMoney,createTime,updateTime);
        }
        set.close();
        statement.close();
        return  bankAccountEntity;
    }

    @Override
    public boolean insert(BankAccountEntity bankAccountEntity) throws SQLException {
        return false;
    }

    @Override
    public boolean deleteById(Integer id) throws SQLException {
        return false;
    }

    @Override
    public boolean update(BankAccountEntity bankAccountEntity) throws SQLException {
        return false;
    }

    public boolean updateAccount(Connection connection, String name, BigDecimal money) throws SQLException {
        boolean flag = false;
        String sql = "update bank_account set account_money = " +
                "account_money + ? where account_name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setBigDecimal(1,money);
        statement.setString(2,name);
        if(statement.executeUpdate()>0){
            flag = true;
        }

        statement.close();
        return flag;
    }
}
